/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import database_beans.Korisnik;
import database_beans.Role;
import database_beans.Student;
import database_beans.Zaposleni;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import server.Log;
import server.UserLoginAndLogout;
import server.Util;

/**
 *
 * @author stefan
 */
@ManagedBean( name = "userBean")
@SessionScoped
public class UserBean implements Serializable{

    private int idKorisnik;
    private Role role;
    private String korisnickoIme;
    private String lozinka;
    private String ime;
    private String prezime;
    private Korisnik korisnik;
    private Zaposleni zaposleni;
    private Student student;
    
    private boolean isStudent;
    private boolean isAdmin;
    private boolean isNastavnik;
    private boolean renderLogin = true;
    
    private String oldPassword;
    private String newPassword;
    private String reEnteredNewPassword;

    public UserBean() {
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
//        switch((int)session.getAttribute(Util.SESSION_USER_ID)){
//            case 1:
//                
//                break;
//            case 2:
//                zaposleni = (Zaposleni)session.getAttribute(Util.SESSION_USER);
//                break;
//            case 3:
//                student = (Student)session.getAttribute(Util.SESSION_USER);
//                break;
//        }
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    
    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getReEnteredNewPassword() {
        return reEnteredNewPassword;
    }

    public void setReEnteredNewPassword(String reEnteredNewPassword) {
        this.reEnteredNewPassword = reEnteredNewPassword;
    }
    
    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getIme() {
        return ime;
    }

    public void setIme(String ime) {
        this.ime = ime;
    }

    public String getLozinka() {
        return lozinka;
    }

    public void setLozinka(String lozinka) {
        this.lozinka = lozinka;
    }

    public int getIdKorisnik() {
        return idKorisnik;
    }

    public void setIdKorisnik(int idKorisnik) {
        this.idKorisnik = idKorisnik;
    }

    public Zaposleni getZaposleni() {
        return zaposleni;
    }

    public void setZaposleni(Zaposleni zaposleni) {
        this.zaposleni = zaposleni;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String getKorisnickoIme() {
        return korisnickoIme;
    }

    public void setKorisnickoIme(String korisnickoIme) {
        this.korisnickoIme = korisnickoIme;
    }

    public String getPrezime() {
        return prezime;
    }

    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }

    public String getImePrezime() {
        return "" + ime + " " + prezime;

    }

    public boolean isIsStudent() {
        return isStudent;
    }

    public void setIsStudent(boolean isStudent) {
        this.isStudent = isStudent;
    }

    public boolean isIsAdmin() {
        return isAdmin;
    }

    public void setIsAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }

    public boolean isIsNastavnik() {
        return isNastavnik;
    }

    public void setIsNastavnik(boolean isNastavnik) {
        this.isNastavnik = isNastavnik;
    }    

    public boolean isRenderLogin() {
        return renderLogin;
    }

    public void setRenderLogin(boolean renderLogin) {
        this.renderLogin = renderLogin;
    }
    
    
    
    public String attempLogin() {
        
        String retPage = "";
        ////////////////////////////////////////
        korisnik = UserLoginAndLogout.getInstance().login(ime ,lozinka);
        if (korisnik != null) {
            
            Session session = Util.getSesion();
            Transaction t = session.beginTransaction();
            try{
            role = korisnik.getRole();
            ime = korisnik.getIme();
            prezime = korisnik.getPrezime();
            Log.i("ID role je " + role.getIdRole());
            if (role.getIdRole() == 1) {// ADMIN
               // Criteria cr = session.createCriteria(Zaposleni.class);
                //cr.add(Restrictions.eq("idKorisnik", user.getIdKorisnik()));
                //Log.i("ID korsnika je " + user.getIdKorisnik());
               // if (!cr.list().isEmpty()) {
                    //zaposleni = (Zaposleni) cr.list().get(0);
                    isAdmin = true;
                    renderLogin = false;
                    retPage = "pocetna?faces-redirect=true";
              //  }
            } else if (role.getIdRole() == 2) {
                Criteria cr = session.createCriteria(Zaposleni.class);
                cr.add(Restrictions.eq("idKorisnik", korisnik.getIdKorisnik()));
                Log.i("ID korsnika je " + korisnik.getIdKorisnik());
                if (!cr.list().isEmpty()) {
                    zaposleni = (Zaposleni) cr.list().get(0);
                    isNastavnik = true;
                    renderLogin = false;
                    retPage = "profilZaposleni?faces-redirect=true";
                }
            } else {
                Criteria cr = session.createCriteria(Student.class);
                cr.add(Restrictions.eq("idKorisnik", korisnik.getIdKorisnik()));
                Log.i("ID korsnika je " + korisnik.getIdKorisnik());
                if (!cr.list().isEmpty()) {
                    student = (Student) cr.list().get(0); 
                    isStudent = true;
                    renderLogin = false;
                    retPage = "pocetna?faces-redirect=true";
                }
            }
            }catch(Exception e){
                t.rollback();
            }finally{
                t.commit();
                Log.i("Commit na LOGINU");
            }
        }else{
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Uneli ste neispravne login podatke!!!"));
        }
        
        return retPage;
    }
    
    public String attemptLogout(){
        
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession)context.getExternalContext().getSession(false);
        session.invalidate();
        isAdmin = isStudent = isNastavnik = false;
        renderLogin = true;
        return "pocetna?faces:redirect=true";
    }
    
    public void changePassword(){
        if(oldPassword.equals(korisnik.getLozinka())){
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try{
            korisnik.setLozinka(newPassword);
            session.update(korisnik);
            t.commit();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Unspesno sacuvana lozinka!"));
        }catch(Exception e){
            t.rollback();
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska!!! Lozinka nije sacuvana"));
        }
    }else{
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Uneli ste pogresnu lozinku!!!"));
        }
    }
}
